#include "lightingdialog.h"
#include "ui_lightingdialog.h"

#include <QPainter>
#include <QDebug>

LightingDialog::LightingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LightingDialog)
{
    ui->setupUi(this);

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint & ~Qt::WindowMinimizeButtonHint);
    setWindowTitle(tr("Lighting"));
    setModal(true);
    setFixedSize(300, 340);

    m_scene = new QGraphicsScene(this);
    m_scene->setBackgroundBrush(Qt::darkGray);
    ui->graphicsView->setScene(m_scene);
    ui->graphicsView->setFixedSize(239, 203);
    m_pixmapItem = new QGraphicsPixmapItem;
    m_scene->addItem(m_pixmapItem);

    m_lightOrigin = new QGraphicsEllipseItem(0, 0, 10, 10);
    m_scene->addItem(m_lightOrigin);

    connect(ui->applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClicked()));
    connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));
    connect(ui->intensitySlider, SIGNAL(valueChanged(int)), ui->intensitySpinBox, SLOT(setValue(int)));
    connect(ui->intensitySpinBox, SIGNAL(valueChanged(int)), this, SLOT(parameterChanged(int)));
    connect(ui->radiusSlider, SIGNAL(valueChanged(int)), ui->radiusSpinBox, SLOT(setValue(int)));
    connect(ui->radiusSpinBox, SIGNAL(valueChanged(int)), this, SLOT(parameterChanged(int)));
    connect(ui->xSpinBox, SIGNAL(valueChanged(int)), this, SLOT(parameterChanged(int)));
    connect(ui->ySpinBox, SIGNAL(valueChanged(int)), this, SLOT(parameterChanged(int)));
}

LightingDialog::~LightingDialog()
{
    delete ui;
}

void LightingDialog::setPixmap(QImage image)
{
    QImage temp = image.scaled(200, 180, Qt::KeepAspectRatio);
    m_pixmapItem->setPixmap(QPixmap::fromImage(temp));
    m_widthScaled = image.width() / (double)temp.width();
    m_heightScaled = image.height() / (double)temp.height();
}

void LightingDialog::setPosSize(int width, int height)
{
    ui->xSpinBox->setMaximum(width + 10);
    ui->ySpinBox->setMaximum(height + 10);
    ui->xSpinBox->setValue(width / 2);
    ui->ySpinBox->setValue(height / 2);
}

void LightingDialog::applyButtonClicked()
{
    int intensity = ui->intensitySlider->value();
    int radius = ui->radiusSlider->value();
    int x = ui->xSpinBox->value();
    int y = ui->ySpinBox->value();

    QString str = "Lighting Filter. Intensity : " + QString::number(intensity)
            + ", Light Radius : " + QString::number(radius)
            + ", Light Position : (" + QString::number(x)
            + "," + QString::number(y) + ").";

    emit applyOperator(str);
    close();
}

void LightingDialog::cancelButtonClicked()
{
    close();
}

void LightingDialog::closeEvent(QCloseEvent *)
{
    emit cancelOperator();
}

void LightingDialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(Qt::gray);
    painter.drawRect(10, 10, width() - 20, height() - 20);
}

void LightingDialog::showEvent(QShowEvent *)
{
    ui->intensitySlider->setValue(0);
    ui->radiusSlider->setValue(10);
}

void LightingDialog::parameterChanged(int param)
{
    QWidget* widget = qobject_cast<QWidget*>(sender());
    if (widget)
    {
        if (widget->objectName() == "intensitySpinBox")
        {
            ui->intensitySlider->setValue(param);
        }
        else if (widget->objectName() == "radiusSpinBox")
        {
            ui->radiusSlider->setValue(param);
        }
        else if (widget->objectName() == "xSpinBox")
        {
            ui->xSpinBox->setValue(param);
        }
        else if (widget->objectName() == "ySpinBox")
        {
            ui->ySpinBox->setValue(param);
        }

        int intensity = ui->intensitySlider->value();
        int radius = ui->radiusSlider->value();
        int x = ui->xSpinBox->value();
        int y = ui->ySpinBox->value();
        double realX = x / m_widthScaled;
        double realY = y / m_heightScaled;

        m_lightOrigin->setPos(m_pixmapItem->pos() + QPointF(realX, realY));

        emit parametersChanged(QPoint(x, y), intensity, radius);
    }
}
